﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.Case;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.Case
{
    /// <summary>
    /// 出入库单据服务
    /// </summary>
    public class StockBillService:BasicService<StockBill>
    {
        public StockBillService(ISession session)
            : base(session)
        {

        }

        public override void SaveOrUpdate(StockBill entity)
        {
            if (entity.Id == 0)
            {
                int billDate = int.Parse(DateTime.Now.ToString("yyMMdd"));
                int billType = (int)entity.BillType;

                string serialNo = this.mSession.CreateSQLQuery(string.Format("exec Ev_UpdateBillSerialNo {0},{1}", billType, billDate)).UniqueResult<string>();
                entity.SerialNo = serialNo;
            }
            base.SaveOrUpdate(entity);

        }

        public void CheckBill(int billId, int orgId, int checkerId,ITransaction tx)
        {

            IDbCommand cmd = mSession.Connection.CreateCommand();

            cmd.CommandText = "Ev_CheckStockBill";

            cmd.CommandType = CommandType.StoredProcedure;

            tx.Enlist(cmd);

            IDataParameter p1 = cmd.CreateParameter();
            p1.ParameterName = "@billId";
            p1.DbType = DbType.Int32;
            p1.Value = billId;
            cmd.Parameters.Add(p1);

            IDataParameter p2 = cmd.CreateParameter();
            p2.ParameterName = "@deptId";
            p2.DbType = DbType.Int32;
            p2.Value = orgId;
            cmd.Parameters.Add(p2);

            IDataParameter p3 = cmd.CreateParameter();
            p3.ParameterName = "@checkerId";
            p3.DbType = DbType.Int32;
            p3.Value = checkerId;
            cmd.Parameters.Add(p3);

            IDataParameter p4 = cmd.CreateParameter();
            p4.ParameterName = "@errorId";
            p4.DbType = DbType.Int32;
            p4.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(p4);

            cmd.ExecuteNonQuery();

            //object obj = cmd.Parameters["@errorId"];

        }
    }
}
